package com.foxzzz.two.pointers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Solution350 {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int j = 0;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums1.length; i++) {
            while (j < nums2.length && nums1[i] > nums2[j]) {
                j++;
            }
            if (j == nums2.length) {
                break;
            }
            if (nums1[i] == nums2[j]) {
                j++;
                list.add(nums1[i]);
            }
        }
        int res[] = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    public static void main(String[] args) {
        new Solution350().intersect(new int[]{1, 2, 2, 1}, new int[]{2, 2});
        new Solution350().intersect(new int[]{}, new int[]{});
    }
}
